/**
 * User路由模块
 * routes/users.js
 */
var express = require('express');
var router = express.Router();
const users = require("../controller/users");
const auth = require("../controller/auth");

/**
 * @swagger
 * /users/register:
 *   post:
 *     tags:
 *       - Users
 *     summary: 用户注册
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *               password:
 *                 type: string
 *               email:
 *                 type: string
 *             example:
 *               username: "zs453sd77fsd24"
 *               password: "P@sffd254123"
 *               email: "zhangafsad@campus.com"
 *     responses:
 *       200:
 *         description: 注册成功
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器内部错误
 */
router.post("/register",users.register);

/**
 * @swagger
 * /users/findall:
 *   get:
 *     tags:
 *       - Users
 *     summary: 查询所有用户
 *     parameters:
 *       - in: query
 *         name: page
 *         required: true
 *         schema:
 *           type: integer
 *           example: 1
 *       - in: query
 *         name: limit
 *         required: true
 *         schema:
 *           type: integer
 *           example: 2
 *     responses:
 *       200:
 *         description: 成功获取用户列表
 *       500:
 *         description: 服务器内部错误
 */
router.get('/findall',auth.isLogin,auth.isAdmi,users.findAll);

/**
 * @swagger
 * /users/findbyid/{user_id}:
 *   get:
 *     tags:
 *       - Users
 *     summary: 根据ID查询用户
 *     parameters:
 *       - in: path
 *         name: user_id
 *         example:
 *           1
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: 成功获取用户信息
 *       404:
 *         description: 用户未找到
 *       500:
 *         description: 服务器内部错误
 */
router.get('/findbyid/:user_id',auth.isLogin,auth.isSelfOrAdmi,users.findById);

/**
 * @swagger
 * /users/update:
 *   put:
 *     tags:
 *       - Users
 *     summary: 更新用户信息
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               user_id:
 *                 type: integer
 *                 example: 5
 *               username:
 *                 type: string
 *                 example: "kfc"
 *               email:
 *                 type: string
 *                 example: "crazyday4@kfc.com"
 *     responses:
 *       200:
 *         description: 更新成功
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器内部错误
 */
router.put('/update',auth.isLogin,auth.isSelfOrAdmi,users.update);

router.put("/avatar",auth.isLogin,auth.isSelfOrAdmi,users.avatar)

/**
 * @swagger
 * /users/remove/{id}:
 *   delete:
 *     tags:
 *       - Users
 *     summary: 删除用户
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: 删除成功
 *       404:
 *         description: 用户未找到
 *       500:
 *         description: 服务器内部错误
 */
router.delete("/remove/:user_id",auth.isLogin,auth.isAdmi,users.remove);

/**
 * @swagger
 * /users/login:
 *   post:
 *     tags:
 *       - Users
 *     summary: 用户登录
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *                 example: "admin"
 *               password:
 *                 type: string
 *                 example: "123456"
 *     responses:
 *       200:
 *         description: 成功获取用户信息
 *       404:
 *         description: 用户未找到
 *       500:
 *         description: 服务器内部错误
 */
router.post("/login", users.login);

/**
 * @swagger
 * /users/logout/{user_id}:
 *   post:
 *     tags:
 *       - Users
 *     summary: 退出登录
 *     description: 用户退出登录，清除会话或令牌信息
  *     parameters:
 *       - in: path
 *         name: user_id
 *         example:
 *           1
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: 成功获取用户信息
 *       404:
 *         description: 用户未找到
 *       500:
 *         description: 服务器内部错误
 */
router.post("/logout/:user_id",auth.isLogin,auth.isSelfOrAdmi,users.logout);

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
  next();
});
 
module.exports = router;